package com.wephone.Student.ui;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.wephone.Student.R;
import com.wephone.Student.bean.data.CarInfoData;
import com.wephone.Student.bean.data.MyLocationInfo;
import com.wephone.Student.huanxin.activity.ChatActivity;
import com.wephone.Student.util.Constants;

public class MutualTransaction2DetailActivity extends BaseActivity {
	public static CarInfoData data;
	private TextView detailTitle;
	private TextView detailInput1;
	private TextView detailInput2;
	private TextView detailInput3;
	private TextView detailInput4;
	private TextView detailInput5;
	private TextView detailInput6;
	private TextView detailInput7;
	private TextView detailInput8;
	private TextView detailInput9;
	private ImageView itemImage;
	private TextView userText1;
	private TextView userText2;
	private ImageView subPic1;
	private ImageView subPic2;
	private ImageView subPic3;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mutual_transaction_detail_2);
		
		init();
	}
	
	private void init(){
		detailTitle = (TextView)findViewById(R.id.detail_title);
		detailInput1 = (TextView)findViewById(R.id.fly_detail_input_1);
		detailInput2 = (TextView)findViewById(R.id.fly_detail_input_2);
		detailInput3 = (TextView)findViewById(R.id.fly_detail_input_3);
		detailInput4 = (TextView)findViewById(R.id.fly_detail_input_4);
		detailInput5 = (TextView)findViewById(R.id.fly_detail_input_5);
		detailInput6 = (TextView)findViewById(R.id.fly_detail_input_6);
		detailInput7 = (TextView)findViewById(R.id.fly_detail_input_7);
		detailInput8 = (TextView)findViewById(R.id.fly_detail_input_8);
		detailInput9 = (TextView)findViewById(R.id.fly_detail_input_9);
		userText1 = (TextView)findViewById(R.id.user_text_1);
		userText2 = (TextView)findViewById(R.id.user_text_2);
		itemImage = (ImageView)findViewById(R.id.item_image);
		subPic1 = (ImageView)findViewById(R.id.item_info_pic_1);
		subPic2 = (ImageView)findViewById(R.id.item_info_pic_2);
		subPic3 = (ImageView)findViewById(R.id.item_info_pic_3);
		
		findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		//点击图片大图显示
		subPic1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showBigPictureDialog(getApplicationContext(), 0);
			}
		});
		subPic2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showBigPictureDialog(getApplicationContext(), 1);
			}
		});
		subPic3.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showBigPictureDialog(getApplicationContext(), 2);
			}
		});
		
		detailTitle.setText(data.getcTitle());
		if(!TextUtils.isEmpty(data.getuInfo().getuLocDetail())){
			Gson gson = new Gson();
			MyLocationInfo locInfo = gson.fromJson(data.getuInfo().getuLocDetail(), MyLocationInfo.class);
			if(locInfo.getType().equals("Foreign")){
				userText1.setText(locInfo.getSchool());
				userText2.setText(locInfo.getCountry());
			}else if(locInfo.getType().equals("Returnees")){
				userText1.setText(locInfo.getSchool1());
				userText2.setText(locInfo.getCountry());
			}else if(locInfo.getType().equals("PlanGoOut")){
				userText1.setText(locInfo.getToschool());
				userText2.setText(locInfo.getCountry());
			}
		}
		if(data.getcType().equals("0")){
			detailInput1.setText("出售");
		}else{
			detailInput1.setText("求购");
		}
		detailInput2.setText(data.getcLoc());
		if(TextUtils.isEmpty(data.getcPrice()) || Double.parseDouble(data.getcPrice())<=0){
			detailInput3.setText("面议");
		}else{
			detailInput3.setText(data.getcPrice());
		}
		detailInput4.setText(data.getcBrands());
		detailInput5.setText(data.getcMiles()+"公里");
//		System.out.println("-----buyTime-----"+data.getcBuyTime());
		try {
			Date date = new Date(Long.parseLong(data.getcBuyTime())*1000);
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日");
			detailInput6.setText(sdf2.format(date));
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(data.getcType().equals("0")){
			detailInput7.setText("手动档");
		}else{
			detailInput7.setText("自动档");
		}
		detailInput8.setText(data.getcDisplacement());
		detailInput9.setText(data.getcDetail());
		
		if(!TextUtils.isEmpty(data.getcImg1())){
			imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getcImg1(), subPic1);
//			String endStr = data.getcImg1().substring(data.getcImg1().lastIndexOf("."));
//			imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getcImg1().replace(endStr, "_150_150"+endStr), subPic1);
		}
		if(!TextUtils.isEmpty(data.getcImg2())){
			imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getcImg2(), subPic2);
//			String endStr = data.getcImg2().substring(data.getcImg2().lastIndexOf("."));
//			imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getcImg2().replace(endStr, "_150_150"+endStr), subPic2);
		}
		if(!TextUtils.isEmpty(data.getcImg3())){
			imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getcImg3(), subPic3);
//			String endStr = data.getcImg3().substring(data.getcImg3().lastIndexOf("."));
//			imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getcImg3().replace(endStr, "_150_150"+endStr), subPic3);
		}
		if(!TextUtils.isEmpty(data.getuInfo().getuIcon())){
			DisplayImageOptions options = new DisplayImageOptions.Builder()
	        .showStubImage(R.drawable.default_user_logo)
	        .showImageOnFail(R.drawable.default_user_logo)
	        .bitmapConfig(Bitmap.Config.ARGB_8888)
	        .cacheInMemory()
	        .cacheOnDisc()
	        .displayer(new RoundedBitmapDisplayer(10))
	        .build();
//			imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getuInfo().getuIcon(), itemImage, options);
			String endStr = data.getuInfo().getuIcon().substring(data.getuInfo().getuIcon().lastIndexOf("."));
			imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getuInfo().getuIcon().replace(endStr, "_150_150"+endStr), itemImage, options);
		}
		itemImage.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Bundle bundle = new Bundle();
				bundle.putSerializable("userData", data.getuInfo());
				startActivity(new Intent(getApplicationContext(),UserInfoDetailActivity.class).putExtras(bundle));
			}
		});
		
		findViewById(R.id.btn_bottom_call).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(!TextUtils.isEmpty(data.getcTel())){
					Uri uri = Uri.parse("tel:"+data.getcTel());
					Intent it = new Intent(Intent.ACTION_DIAL, uri);
					startActivity(it);
				}else{
					Toast.makeText(getApplicationContext(), "抱歉，该用户没有留下联系方式", Toast.LENGTH_SHORT).show();
				}
			}
		});
		findViewById(R.id.btn_bottom_chat).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				MyMessageActivity.toUserId = data.getuInfo().getuId();
//				MyMessageActivity.toUserName = data.getuInfo().getuName();
//				MyMessageActivity.toUserInfo = data.getuInfo();
//				startActivity(new Intent(getApplicationContext(),MyMessageActivity.class));
				
				ChatActivity.toUserInfo = data.getuInfo();
				Intent intent = new Intent(getApplicationContext(),ChatActivity.class);
				intent.putExtra("chatType", ChatActivity.CHATTYPE_SINGLE);
				intent.putExtra("userId", data.getuInfo().getuId());
				startActivity(intent);
			}
		});
		
		if(BaseActivity.getMyInfo(getApplicationContext()).getuId().equals(data.getuId())){
			findViewById(R.id.bottom_button_layout).setVisibility(View.GONE);
		}else{
			findViewById(R.id.bottom_button_layout).setVisibility(View.VISIBLE);
		}
	}
	
	private void showBigPictureDialog(Context context,final int idx){
		String showImgUrl = "";
		if(idx==0){
			if(!TextUtils.isEmpty(data.getcImg1())){
				showImgUrl = Constants.IMAGE_PHOTO_SERVER_PATH+data.getcImg1();
			}
		}else if(idx==1){
			if(!TextUtils.isEmpty(data.getcImg2())){
				showImgUrl = Constants.IMAGE_PHOTO_SERVER_PATH+data.getcImg2();
			}
		}else if(idx==2){
			if(!TextUtils.isEmpty(data.getcImg3())){
				showImgUrl = Constants.IMAGE_PHOTO_SERVER_PATH+data.getcImg3();
			}
		}
		if(!TextUtils.isEmpty(showImgUrl)){
			startActivity(new Intent(getApplicationContext(),GalleryUrlActivity.class).putExtra("imgUrl", showImgUrl));
		}
	}

}
